package bean.managed;

import entity.PaymentEntity;
import bean.managed.util.JsfUtil;
import bean.session.PaymentEntityFacade;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;

@ManagedBean(name = "paymentEntityController")
@SessionScoped
public class PaymentEntityController implements Serializable {

    private PaymentEntity current;
    private List<PaymentEntity> allPayments;
    private List<PaymentEntity> ciaPayments;
    private List<PaymentEntity> normalPayments;
    private List<PaymentEntity> latePayments;
    
    @EJB
    private bean.session.PaymentEntityFacade ejbFacade;


    public PaymentEntityController() {
    }

    public PaymentEntity getSelected() {
        if (current == null) {
            current = new PaymentEntity();
        }
        return current;
    }

    private PaymentEntityFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PaymentEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PaymentEntityUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        performDestroy();
        return "List";
    }


    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PaymentEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = PaymentEntity.class)
    public static class PaymentEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            PaymentEntityController controller = (PaymentEntityController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "paymentEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof PaymentEntity) {
                PaymentEntity o = (PaymentEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + PaymentEntityController.class.getName());
            }
        }
    }

    public List<PaymentEntity> getAllPayments() {
        allPayments = ejbFacade.findAll();
        return allPayments;
    }

    public void setAllPayments(List<PaymentEntity> allPayments) {
        this.allPayments = allPayments;
    }

    public List<PaymentEntity> getCiaPayments() {
        ciaPayments = ejbFacade.getAllCIAPayments();
        return ciaPayments;
    }

    public void setCiaPayments(List<PaymentEntity> ciaPayments) {
        this.ciaPayments = ciaPayments;
    }

    public List<PaymentEntity> getNormalPayments() {
        normalPayments = ejbFacade.getAllNormalPayments();
        return normalPayments;
    }

    public void setNormalPayments(List<PaymentEntity> normalPayments) {
        this.normalPayments = normalPayments;
    }

    public List<PaymentEntity> getLatePayments() {
        latePayments = ejbFacade.getAllReminderPayments();
        return latePayments;
    }

    public void setLatePayments(List<PaymentEntity> latePayments) {
        this.latePayments = latePayments;
    }
    
}
